package com.ysstech.kettle.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.JsonUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.kettle.entity.*;
import com.ysstech.kettle.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 采集任务信息表 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-10-25
 */
@Controller
@Slf4j
@Api(tags = "采集任务管理api")
@RequestMapping("/jobDuties")
public class JobDutiesController extends BaseController {

    @Autowired
    private JobDutiesService jobDutiesService;
    @Autowired
    private JobCategoryService jobCategoryService;
    @Autowired
    private JobDatasourceService jobDatasourceService;
    @Autowired
    private JobRelationService jobRelationService;
    @Autowired
    private JobDutiesTaskService jobDutiesTaskService;

    @YssPermissions(value = {"kettle:job:duties"})
    @ApiOperation(value = "跳转采集任务管理页面")
    @YssLog(module = "采集任务管理", remark = "跳转采集任务管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "kettle/jobDuties";
    }

    @YssPermissions(value = {"kettle:job:duties"})
    @ApiOperation(value = "获取采集任务页面初始值")
    @YssLog(module = "采集任务管理", remark = "获取采集任务管理页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        //查询所有采集分类信息
        QueryWrapper<JobCategory> jobCategoryQueryWrapper = new QueryWrapper<>();
        jobCategoryQueryWrapper.eq("STATUS", 1);
        jobCategoryQueryWrapper.orderByAsc("SEQUENCE");
        List<JobCategory> jobCategorylist = jobCategoryService.list(jobCategoryQueryWrapper);
        map.put("jobCategorylist", jobCategorylist);
        //获取所有数据库连接
        QueryWrapper<JobDatasource> jobDatasourceQueryWrapper = new QueryWrapper<>();
        jobDatasourceQueryWrapper.eq("STATUS", 1);
        List<JobDatasource> jobDatasourceList = jobDatasourceService.list(jobDatasourceQueryWrapper);
        map.put("jobDatasourceList", jobDatasourceList);
        // 所有关联关系
        QueryWrapper<JobRelation> jobRelationQueryWrapper = new QueryWrapper<>();
        jobDatasourceQueryWrapper.eq("STATUS", 1);
        List<JobRelation> jobRelationList = jobRelationService.list(jobRelationQueryWrapper);
        map.put("jobRelationList", jobRelationList);
        //查询所有采集任务信息
        QueryWrapper<JobDuties> jobDutiesQueryWrapper = new QueryWrapper<>();
        jobDutiesQueryWrapper.eq("STATUS", 1);
        jobDutiesQueryWrapper.orderByAsc("SEQUENCE");
        List<JobDuties> jobDutiesList = jobDutiesService.list(jobDutiesQueryWrapper);
        map.put("jobDutiesList", jobDutiesList);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }

    /**
     * 获取采集任务信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:duties"})
    @ApiOperation(value = "获取采集任务信息")
    @YssLog(module = "采集任务管理", remark = "获取采集任务信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(JobDuties jobDuties, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取采集任务信息成功",
                jobDutiesService.queryAllJobDuties(jobDuties, this.getPage(page, rows)));
    }

    /**
     * 新增采集任务的方法
     *
     * @param jobDuties
     * @return
     */
    @YssPermissions(value = {"kettle:job:duties:add"})
    @ApiOperation(value = "新增采集任务信息")
    @YssLog(module = "采集任务管理", remark = "新增采集任务信息")
    @RequestMapping(value = "/addJobDuties")
    @ResponseBody
    public RestResult addJobDuties(JobDuties jobDuties) throws Exception {
        jobDuties.setId(DateUtil.generateUUID());
        jobDuties.setAddTime(LocalDateTime.now());
        jobDutiesService.save(jobDuties);
        return ResultGenerator.getSuccessResult("新增采集任务信息成功！");
    }

    /**
     * 修改采集任务信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:duties:edit"})
    @ApiOperation(value = "修改采集任务信息")
    @YssLog(module = "采集任务管理", remark = "修改采集任务信息")
    @RequestMapping(value = "/editJobDuties")
    @ResponseBody
    public RestResult editJobDuties(JobDuties jobDuties) throws Exception {
        if (jobDuties == null || StringUtils.isBlank(jobDuties.getId())) {
            throw new BusinessException("获取采集任务信息失败，请稍后重试！");
        }
        jobDuties.setEditTime(LocalDateTime.now());
        jobDutiesService.updateById(jobDuties);
        return ResultGenerator.getSuccessResult("修改采集任务信息成功！");
    }

    /**
     * 删除采集任务信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:duties:del"})
    @ApiOperation(value = "删除采集任务信息")
    @YssLog(module = "采集任务管理", remark = "删除采集任务信息")
    @RequestMapping(value = "/delJobDuties")
    @ResponseBody
    public RestResult delJobDuties(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除采集任务信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("获取删除采集任务信息失败，请稍后重试！");
        }
        jobDutiesService.removeByIds(list);
        //删除采集任务下的作业任务关联关系
        QueryWrapper<JobDutiesTask> wrapper = new QueryWrapper();
        wrapper.eq("DUTIES_ID", list);
        jobDutiesTaskService.remove(wrapper);
        return ResultGenerator.getSuccessResult("删除采集任务信息成功！");
    }
}
